<template>
    <!-- 头部 -->
    <van-nav-bar title="创建订单" left-arrow @click-left="$router.back()">
        <template #right>
            <van-icon name="ellipsis" />
        </template>
    </van-nav-bar>
    <!-- 地址 -->
    <router-link to="/addressList">
        <div class="address-wrap">
            <div class="name">
                <span>{{ receiver.name }}</span>
                <span>{{ receiver.tel }}</span>
            </div>
            <van-icon name="arrow" />
            <div class="address">
                <span>{{ receiver.address }}</span>
            </div>
        </div>
    </router-link>
    <!-- 订单 -->
    <van-card v-for="item in cartCheckedGoods" :key="item.cartItemId" :num="item.goodsCount" :price="item.sellingPrice"
        :title="item.goodsName" :thumb="item.goodsCoverImg" />
    <!--提交订单栏  -->
    <van-submit-bar :price="tatol" button-text="提交订单" @submit="onSubmit" />

    <!-- 底部弹出层 -->
    <van-popup v-model:show="show" round position="bottom" :style="{ height: '30%' }">
        <template #default>
            <div class="box-pay">
                <div class="pay" @click="payOrder(item)" v-for="(item, i) in payWay" :key="i">{{ item.name }}</div>
                <section></section>
                <div @click="canelOrder" class="canel">取消</div>
            </div>
        </template>
    </van-popup>

</template>
<script setup>
import { showNotify } from 'vant';
import { getAddressId, getOrder, addOrder, paySuccess } from "@/api/index.js"
import { onMounted, reactive, ref, computed } from "vue";
import { useRoute, useRouter } from "vue-router"
let route = useRoute()
let router = useRouter()
let receiver = reactive({
    name: '',//收货人姓名 
    tel: '',//收货人电话
    address: '',//收货人地址
    order: ''//生成的订单号
})
let cartCheckedIds = ref([])//选中商品的id
let cartCheckedGoods = ref([])//选中商品
let show = ref(false)//底部弹出
let payWay = ref([{ name: '微信支付' }, { name: '支付宝支付' }])
onMounted(() => {
    getData()
    getCartChecked()
})
// 根据商品id获取商品的数据
async function getCartChecked() {
    let ids = localStorage.getItem('cartCheckedIds')
    cartCheckedIds.value = ids
    getOrder(cartCheckedIds.value).then(res => {
        if (res.resultCode === 200) {
            cartCheckedGoods.value = res.data
        }
    })
}
// 总价
let tatol = computed(() => {
    return cartCheckedGoods.value.reduce((prev, next) => {
        return prev + next.sellingPrice * 100 * next.goodsCount
    }, 0)
})
// 地址
async function getData() {
    let res = await getAddressId(route.query.addressId)
    receiver.name = res.data.userName;
    receiver.tel = res.data.userPhone;
    receiver.address = res.data.provinceName + res.data.regionName
        + res.data.detailAddress;
}
//提交订单
let onSubmit = () => {
    show.value = !show.value
    addOrder(route.query.addressId, cartCheckedIds.value.split(',')).then(res => {
        if (res.resultCode == 200) {
            receiver.order = res.data
        }
    })
}
// 支付
function payOrder(item) {
    if (item.name === '微信支付') {
        paySuccess(receiver.order, 1).then(res => {
            if (res.resultCode == 200) {
                showNotify({ message: '支付成功' });
                setTimeout(() => {
                    router.push("/order")
                }, 500)
            }
        })
    } else if (item.name === '支付宝支付') {
        paySuccess(receiver.order, 1).then(res => {
            if (res.resultCode == 200) {
                showNotify({ message: '支付成功' });
                setTimeout(() => {
                    router.push("/order")
                }, 500)
            }
        })
    }
}
// 取消
function canelOrder(item) {
    setTimeout(() => {
        router.push("/order")
    }, 500)
}
</script>
<style lang="less" scoped>
.van-nav-bar {
    width: 100%;
}

.address-wrap {
    width: 100%;
    margin-bottom: 0.53333rem;
    background: #fff;
    position: relative;
    font-size: 0.37333rem;
    padding: 0.63rem 0.4rem 0.66667rem;
    color: #222333;
    box-sizing: border-box;

    .name {
        display: flex;
        justify-content: start;
        margin: 0.26667rem 0 0.27667rem;
        line-height: 0.37333rem;

        span:nth-child(1) {
            margin-right: 0.04rem;
        }
    }

    .address {
        display: flex;
        justify-content: left;
        line-height: 0.37333rem;
    }

    .van-icon {
        position: absolute;
        right: 0.26667rem;
        top: 50%;
        -webkit-transform: translateY(-50%);
        transform: translateY(-50%);
        font-size: 0.53333rem;
    }

}

.address-wrap::before {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    height: 0.05333rem;
    background: -webkit-repeating-linear-gradient(135deg,
            #ff6c6c,
            #ff6c6c 20%,
            transparent 0,
            transparent 25%,
            #1989fa 0,
            #1989fa 45%,
            transparent 0,
            transparent 50%);
    background: repeating-linear-gradient(-45deg,
            #ff6c6c,
            #ff6c6c 20%,
            transparent 0,
            transparent 25%,
            #1989fa 0,
            #1989fa 45%,
            transparent 0,
            transparent 50%);
    background-size: 2.13333rem;
    content: "";
}

.van-card {
    width: 100%;
    margin-right: 10px;
}

.box-pay {
    padding: 10px 10px;
    border-radius: 10px;

    .pay {
        margin-top: 10px;
        margin-left: 10px;
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid rgb(221, 218, 218);
    }

    .canel {
        margin-top: 10px;
        margin-left: 10px;
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid rgb(221, 218, 218);
    }
}
</style>